

<!--
  -    Copyright (c) 2018-2025, gewu All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the gewucn.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: gewu
  -->
<template>
  <div class="execution">
    <el-card class="box-card btn-search page-search">
      <div slot="header" class="clearfix">
        <div class="btn-box">
          <el-button
            type="info"
            icon="el-icon-refresh-left"
            @click="refreshChange()"
          ></el-button>
          <el-button
            id="gwButton"
            type="primary"
            style="backgroundcolor: #e1b980"
            icon="el-icon-circle-plus-outline"
            v-if="permissions.ems_emsinsinspectstandard_add"
            @click="$refs.crud.rowAdd()"
            >新增</el-button
          >
          <el-button
            type="success"
            icon="el-icon-edit"
            v-if="permissions.ems_emsinsinspectstandard_edit"
            :disabled="single"
            @click="handleEdit"
            >编辑</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-circle-close"
            v-if="permissions.ems_emsinsinspectstandard_del"
            @click.native="handleDel()"
            :disabled="multiple"
            >删除</el-button
          >
          <el-button type="check" icon="el-icon-download" @click="exportExcel"
            >导出</el-button
          >
        </div>
        <div class="icon-box">
          <i class="el-icon-search" @click="searchShow"></i>
          <i class="el-icon-refresh" @click="refreshChange"></i>
          <i class="el-icon-goods"></i>
          <i class="el-icon-setting" @click="columnShow"></i>
          <i class="icon-zuixiaohua" />
        </div>
      </div>
    </el-card>
    <basic-container>
      <!-- <div class="icon-title">
        <img src="/img/ali/yunwei.png" alt />
        <b>巡检标准 </b>
      </div> -->
      <!-- <IconTitle class="selfTitle" title="巡检标准" imgUrl="yunwei" /> -->
      <avue-crud
        ref="crud"
        v-model="form"
        :page.sync="page"
        :cell-style="cellStyle"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        @selection-change="selectionChange"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @size-change="sizeChange"
        :before-open="handleOpenBefore"
        @current-change="currentChange"
        @row-update="handleUpdate"
        @row-save="handleSave"
        @row-del="handleDel"
        @cell-click="cellClick"
      >
        <template slot="header">
          <IconTitle class="selfTitle" title="巡检标准" imgUrl="yunwei" />
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button type="text" @click="relationDev(scope.row)">
            <i class="el-icon-view"></i>关联设备</el-button
          >
          <el-button type="text" @click="goProject(scope.row)">
            <i class="el-icon-view"></i>点检项</el-button
          >
        </template>
        <template slot="deptId" slot-scope="scope">
          {{ scope.row.deptName }}
        </template>
        <template slot="deptIdForm" slot-scope="scope">
          <avue-input-tree
            v-model="form.deptId"
            :dic="treeDeptData"
            :props="defaultProps"
            placeholder="请选择所属部门"
          />
        </template>
        <template slot="categoryId" slot-scope="scope">
          {{ scope.row.category }}
        </template>
        <template slot="categoryIdForm" slot-scope="scope">
          <avue-input-tree
            v-model="form.categoryId"
            :dic="categoryTreeData"
            :props="defaultProps"
            placeholder="请选择类别"
          />
        </template>
      </avue-crud>
    </basic-container>

    <el-drawer
      class="drawerStyle"
      title="巡检标准"
      :show-close="false"
      :visible.sync="detail"
      direction="rtl"
      size="40%"
      append-to-body
      :modal="false"
    >
      <div>
        <div>
          <span class="labelS">标准编号：</span>
          <span class="contentS">{{ detailInfo.standardNum }}</span>
        </div>
        <div>
          <span class="labelS">标准名称：</span>
          <span class="contentS">{{ detailInfo.standardName }}</span>
        </div>
        <div>
          <span class="labelS">所属部门：</span>
          <span class="contentS">{{detailInfo.deptName}}</span>
        </div>
      </div>
      <div class="line"></div>
      <div>
        <el-card shadow="always" class="box-card">
          <div><span class="tableTitle">关联设备</span></div>
          <el-table :data="tableData1" border style="width: 100%">
            <!-- <el-table-column type="index" width="50" label="序号"> </el-table-column> -->
            <el-table-column prop="deviceNum" label="设备编号" width="150"></el-table-column>
            <el-table-column prop="deviceName" label="设备名称" width="150"></el-table-column>
            <el-table-column prop="category" label="类别" width="100"> </el-table-column>
            <el-table-column prop="brandName" label="品牌" width="100"> </el-table-column>
            <el-table-column prop="specification" label="规格型号" width="200"> </el-table-column>
          </el-table>
          <div style="text-align:right;margin-top:20px;">
                <el-pagination
  background
  :pager-count="5"
  layout="total,prev, pager, next"
 small
 :page-size="pageSize"
 @current-change="handleCurrentChange"
 :current-page.sync="currentPage"
  :total="total">
</el-pagination>
            </div>
        </el-card>
      </div>
    </el-drawer>
    <el-dialog
      title="点检项目管理"
      :visible.sync="dialogspotVisible"
      v-if="dialogspotVisible"
      width="80%"
      class="dialigexecution"
    >
      <Project :params="params" v-if="dialogspotVisible"></Project>
    </el-dialog>
    <el-dialog
      :visible.sync="isRelate"
      width="80%"
      :show-close="false"
      class="relativeDev"
      
    >
      <DeviceRelate @cancel="cancel" v-if="isRelate" @comfire="comfireAdd" :listObj="readyDev"></DeviceRelate>
    </el-dialog>
  </div>
</template>
<script>
import {
  fetchList,
  getObj,
  addObj,
  putObj,
  delObj,
  getRelateDev,
  getDevAnuth
} from "@/api/ems/inspection/criterion";
import {addDev} from "@/api/ems/inspection/dev";
import { tableOption } from "@/const/crud/ems/inspection/criterion";
import { mapGetters } from "vuex";
import jQuery from "jquery";
import IconTitle from "@/components/icon-title/index.vue";
import { fetchTree } from "@/api/admin/dept";
import { categoryTree } from "@/api/ems/equipment/category";
import Project from "../components/spoted.vue";
import DeviceRelate from "../components/deviceRelate";
export default {
  name: "emsinsinspectstandard",
  components: {
    IconTitle,
    Project,
    DeviceRelate,
  },
  data() {
    return {
      detail: false,
      dialogspotVisible: false,
      params: {},
      detailInfo: {},
      tableData: [],
      tableData1:[],
      searchForm: {}, // 查询参数
      single: true, // 非单个禁用
      multiple: true, // 非多个禁用
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条
      },
      currentPage: 1,
      total:0,
      pageSize:5,
      tableLoading: false,
      tableOption: tableOption,
      ids: [],
      selectionList: [],
      treeDeptData: [],
      defaultProps: {
        label: "name",
        value: "id",
      },
      categoryTreeData: [],
      form: {},
      isRelate: false,
      devObject:{},
      readyDev:[]
    };
  },

  computed: {
    ...mapGetters(["permissions", "theme"]),
    permissionList() {
      return {
        addBtn: this.vaildData(
          this.permissions.ems_emsinsinspectstandard_add,
          false
        ),
        delBtn: this.vaildData(
          this.permissions.ems_emsinsinspectstandard_del,
          false
        ),
        editBtn: this.vaildData(
          this.permissions.ems_emsinsinspectstandard_edit,
          false
        ),
      };
    },
  },
  mounted() {
    this.initElement();
    this.changeThme();
  },
  methods: {
    cancel() {
      this.isRelate = false;
    },
    handleCurrentChange(index){
        this.currentPage = index;
         this.getDevDrawe();
    },
    comfireAdd(m){
       addDev({...m,id:this.devObject.id}).then((res)=>{
        if(res.data.code==0){
          this.$message.success("设备添加成功");
          this.isRelate = false;
        }else{
          this.$message.error("设备添加失败")
        }
      })
    },
    cellStyle(data) {
      // 表格的第二列加上小手和padding为0
      if (data.columnIndex === 1) {
        return "color:#02b980;cursor:pointer";
      }
    },
    getDevAnuth(m){
      getDevAnuth({standardId: m}).then((res)=>{
       this.readyDev = res.data.data;
         this.isRelate = true;
      })
    },
    getDevDrawe(){
      getRelateDev({standardId: this.detailInfo.id,size:this.pageSize,current:this.currentPage}).then((res)=>{
            this.tableData1 = res.data.data.records;
            this.total = res.data.data.total;
          })
    },
    cellClick(row, column) {
      if (column.property === "standardNum") {
       
        this.$router.push({
          path: "/ems/inspection/criterion/detail",
          query: {info:row.id},
        });
      } else {
        if (column.label != "操作") {
          this.detailInfo = row;
          this.detail = true;
          this.getDevDrawe();
        }
      }
    },

    initElement() {
      var mediumb = document.createElement("b"); //思路一样引入中间元素
      jQuery(".avue-crud__menu").after(mediumb);
      jQuery(".selfTitle").after(jQuery(".avue-crud__menu"));
      jQuery(mediumb).after(jQuery(".selfTitle"));
      jQuery(mediumb).remove();
    },
    selectionChange(list) {
      this.selectionList = list;
      this.single = list.length !== 1;
      this.multiple = !list.length;
      this.ids = list.map((item) => item.id);
    },
    goLook(row) {
      this.$router.push({
        path: "/ems/inspection/criterion/detail",
        query: { id: row.id },
      });
    },
    goProject(row) {
      this.dialogspotVisible = true;
      this.params = row;
    },
    columnShow() {
      this.$refs.crud.$refs.dialogColumn.columnBox = !0;
    },
    // 搜索框显示与否
    searchShow() {
      this.$refs.crud.$refs.headerSearch.searchShow =
        !this.$refs.crud.$refs.headerSearch.searchShow;
    },

    // 列表查询
    getList(page, params) {
      this.tableLoading = true;
      fetchList(
        Object.assign(
          {
            current: page.currentPage,
            size: page.pageSize,
          },
          params,
          this.searchForm
        )
      )
        .then((response) => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    //编辑
    handleEdit() {
      var refsDate = this.$refs;
      refsDate.crud.rowEdit(
        this.selectionList[0],
        this.selectionList[0].$index
      );
    },
    // 删除
    handleDel: function (row, index) {
      this.$confirm("是否确认删除所选数据项", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let id = "";
          if (row) {
            id = row.id;
          } else {
            id = this.ids;
          }
          return delObj(id);
        })
        .then((data) => {
          this.$message.success("删除成功");
          this.getList(this.page);
        });
    },
    // 更新
    handleUpdate: function (row, index, done, loading) {
      putObj(row)
        .then((data) => {
          this.$message.success("修改成功");
          done();
          this.getList(this.page);
        })
        .catch(() => {
          loading();
        });
    },
    // 保存
    handleSave: function (row, done, loading) {
      addObj(this.form)
        .then((data) => {
          this.$message.success("添加成功");
          done();
          this.getList(this.page);
        })
        .catch(() => {
          loading();
        });
    },
    handleOpenBefore(show, type) {
      window.boxType = type;

      fetchTree().then((response) => {
        this.treeDeptData = response.data.data;
      });
      categoryTree().then((response) => {
        this.categoryTreeData = response.data.data;
      });
      if (type == "add") {
        for (let key in this.form) {
          this.form[key] = undefined;
        }
      }

      show();
    },
    // 每页条数改变事件
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    // 当前页发生改变事件
    currentChange(current) {
      this.page.currentPage = current;
    },
    // 查询事件
    searchChange(form, done) {
      this.searchForm = form;
      this.page.currentPage = 1;
      this.getList(this.page, form);
      done();
    },
    // 刷新事件
    refreshChange() {
      this.getList(this.page);
    },
    // 导出excel
    exportExcel() {
      this.downBlobFile(
        "/ems/emsinsinspectstandard/export",
        this.searchForm,
        "emsinspectionstandard.xlsx"
      );
    },
    // 改变主题颜色
    changeThme() {
      //"#02b980"
      document.getElementById("gwButton").style.backgroundColor = this.theme;
    },
    relationDev(row){
     
      this.devObject=row;
      this.getDevAnuth(row.id);

     
    }
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/drawer.scss";
.icon-title {
  b {
    font-size: 12px;
  }
  .slot {
    font-size: 12px;
    color: #666;
    margin-left: 5px;
  }
  img {
    width: 18px;
    height: 18px;
  }
  display: flex;
  align-items: center;
}
::v-deep .dialigexecution {
  .el-dialog__body {
    padding-top: 0 !important;
  }
}
::v-deep.relativeDev {
  .el-dialog__header {
    padding: 0;
  }
  .el-dialog__body {
    padding-top: 10px;
  }
}
</style>